package com.groovy.domain

import org.junit.Test

/**
 * Groovy的映射
 */
class MapTest {

    //初始化数据
    static def initUsers() {
        def users = [
                "No1": [userName: "周伯通", sex: '男', age: 100],
                "No2": [userName: "瑛姑", sex: '女', age: 67],
                "No3": [userName: "小龙女", sex: '女', age: 30],
                "No4": [userName: "杨过", sex: '男', age: 20],
                "No5": [userName: "黄老邪", sex: '男', age: 60]
        ]
        return users
    }

    @Test
    void testCount() {
        def users = initUsers()

        def count = users.count { def user ->
            //统计年龄大于70岁的用户
            return user.value.age > 70
        }
        println(count)
    }

    @Test
    void testFindAll() {
        def users = initUsers()

        def userNameList = users.findAll { def user ->
            //统计年龄大于60岁的用户
            return user.value.age > 60
        }.collect {
            //返回用户名称
            it.value.userName
        }
        println(userNameList.toListString())
    }

    @Test
    void testGroupBy() {
        def users = initUsers()

        //通过年龄进行分组判断大于60岁的用户
        def groupBy = users.groupBy { return it.value.age >= 60 ? "老人家" : "年轻人" }

        println(groupBy.toString())

    }
}
